import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/page/home/api/api_home.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/router/router_map.gr.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:tender_flutter/widgets/load_state_layout.dart';

class CertificateDetailPage extends StatefulWidget {
  /// 资质信息id
  final String certId;

  /// 资质关联的企业名称
  final String companyName;

  const CertificateDetailPage({Key key, this.certId, this.companyName})
      : super(key: key);

  @override
  _CertificateDetailPageState createState() => _CertificateDetailPageState();
}

class _CertificateDetailPageState extends State<CertificateDetailPage> {
  Map<String, dynamic> detailMaps = HashMap();

  LoadState _loadState = LoadState.State_Loading;

  /// 获取详情
  Future<ApiResponse<void>> _getDetail() async {
    return await ApiHome.getCertificateDetail<void>(
        certId: widget.certId);
  }

  FutureBuilder<ApiResponse<void>> _buildFutureBuilder() {
    return FutureBuilder(
        future: _getDetail(),
        builder:
            (BuildContext context, AsyncSnapshot<ApiResponse<void>> snapshot) {
          if (snapshot.hasData) {
            detailMaps = snapshot.data.originalData;
            _loadState = LoadState.State_Success;
            return LoadStateLayout(
              bgColor: ThemeColors.colorWhite,
              state: _loadState,
              successWidget: _initBody(),
            );
          } else {
            return LoadStateLayout(
              bgColor: ThemeColors.colorWhite,
              state: _loadState,
            );
          }
        });
  }

  @override
  void initState() {
    super.initState();

    /// 页面渲染完成之后的回调，只执行一次
    // WidgetsBinding.instance.addPostFrameCallback((mag) {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.colorWhite,
      appBar: _initAppBar(),
      body: _buildFutureBuilder(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(
      context,
      Text("资质详情", style: FonStyle.navTextStyle()),
      backgroundColor: ThemeColors.colorWhite,
    );
  }

  /// 内容体
  Widget _initBody() {
    return SingleChildScrollView(
      padding: EdgeInsets.symmetric(vertical: ScreenAdapter.height(15)),
      child: Container(
          width: double.infinity,
          child: Column(children: [
            Table(
              //所有列宽
              columnWidths: const {
                //列宽
                0: FixedColumnWidth(150),
                1: FixedColumnWidth(200),
              },
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              //表格边框样式
              border: TableBorder.all(
                width: 0.5,
                color: ThemeColors.colorType47,
                style: BorderStyle.solid,
              ),
              children: detailMaps.keys
                  .map((e) => _getCCell(e, detailMaps[e]))
                  .toList(),
            ),
            Table(
              //所有列宽
              columnWidths: const {
                //列宽
                0: FixedColumnWidth(350),
              },
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              //表格边框样式
              border: TableBorder.all(
                width: 0.5,
                color: ThemeColors.colorType47,
                style: BorderStyle.solid,
              ),
              children: [
                TableRow(children: [
                  Container(
                      padding: EdgeInsets.symmetric(
                          vertical: ScreenAdapter.height(30)),
                      alignment: Alignment.center,
                      child: InkWell(
                          onTap: () {
                            LRouter.navigatorCtx(context).push(
                                Routes.companyDetailPage,
                                arguments: CompanyDetailPageArguments(
                                    companyName: widget.companyName));
                          },
                          child: Text(
                            "查看企业详情",
                            style: TextStyle(
                                color: ThemeColors.colorType6,
                                fontSize: ScreenAdapter.sp(30),
                                fontWeight: FontWeight.w500),
                          )))
                ])
              ],
            ),
          ])),
    );
  }

  TableRow _getCCell(String title, dynamic content) {
    return TableRow(children: [
      Container(
        // height: ScreenAdapter.height(70),
        padding: EdgeInsets.symmetric(
            vertical: ScreenAdapter.height(20),
            horizontal: ScreenAdapter.width(10)),
        alignment: Alignment.center,
        child: Text(
          title,
          style: TextStyle(
              color: ThemeColors.colorType24,
              fontSize: ScreenAdapter.sp(30),
              fontWeight: FontWeight.w500),
        ),
      ),
      Container(
        // height: ScreenAdapter.height(70),
        padding: EdgeInsets.symmetric(
            vertical: ScreenAdapter.height(15),
            horizontal: ScreenAdapter.width(10)),
        alignment: Alignment.center,
        child: Text(
          content.toString(),
          style: TextStyle(
              color: ThemeColors.colorType7,
              fontSize: ScreenAdapter.sp(30),
              fontWeight: FontWeight.w500),
        ),
      ),
    ]);
  }
}
